<template>
  <div class="diy_edit forum_type_edits" id="forum_type_edit">
    <el-form
      :model="form"
      ref="ruleFormRef"
      :rules="rules"
      label-width="auto"
      class="edit_forum_type_form"
      size="large"
    >
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <div class="footer">
        <el-button type="primary" @click="Onsubmit" size="large"
          >提交</el-button
        >
        <el-button @click="cancel()" size="large">取消</el-button>
      </div>
    </el-form>
  </div>
</template>

<script setup>
import { ElMessage } from "element-plus";
import { ref, onMounted } from "vue";
import { $post, $get } from "@/api/Api.js";
import storageHelper from "@/libs/StorageHelper";
import { useStore } from "vuex";
import { useRouter } from "vue-router";
import {
  $get_auth,
  func,
  paramsFun,
  $fullUrl,
  formatDate,
  $banseUrl,
  $check_action,
  $getUrlParams,
  filterSensitiveWords,
} from "@/utils/utils.js";
let router = useRouter();
let store = useStore();
let token = storageHelper.getToken() || null;
let UserInfo = storageHelper.getUserInfo();
let type_id = $getUrlParams().type_id;
let url_add = ref("/api/forum_type/add");
let url_set = ref("/api/forum_type/set");
let url_get_obj = ref("/api/forum_type/get_obj");
let title = ref("forum_type");
let field = ref("type_id");

let form = ref({
  type_id: 0,
  icon: "",
  name: "",
  father_id: 0,
  url: "",
  description: "",
});

let ruleFormRef = ref(null);
let rules = ref({
  name: [
    {
      required: true,
      message: "类型名称不能为空",
      trigger: "blur",
    },
  ],
});

// 点击取消
let cancel = () => {
  router.go(-1);
};
// 点击提交
let Onsubmit = async () => {
  let valid = await ruleFormRef.value.validate();
  if (valid) {
    if(type_id){
        // 编辑
        delete form.value.update_time;
        $post(url_set.value+"?type_id=" + type_id, form.value).then((res) => {
            if (res.result) {
            ruleFormRef.value.resetFields();
            router.push("/home/forum_type/table");
            ElMessage.success("提交成功！");
            } else if (res.error) {
            ElMessage.error(res.error.message);
            }
        });
	 
    }else{
        // 新增
        $post(url_add.value, form.value).then((res) => {
            if (res.result) {
            ruleFormRef.value.resetFields();
            router.push("/home/forum_type/table");
            ElMessage.success("提交成功！");
            } else if (res.error) {
            ElMessage.error(res.error.message);
            }
        });

    }
  } else {
    ElMessage.error(msg);
  }
};
// 获取详情
let get_forum_type_obj = async () => {
  var list = await $get("/api/forum_type/get_obj?type_id=" + type_id);
  form.value = list.result.obj;
};
onMounted(() => {
  if(type_id){
	get_forum_type_obj();
  }
});
</script>

<style scoped lang="less">
.diy_edit {
  width: 100%;
  .edit_forum_type_form {
    max-width: 60%;
    margin: 90px auto;
    .fn {
      width: 100%;
    }
  }
  img {
    width: 100px;
    height: 100px;
  }
  .edit_list_forum_type_sell {
    width: 80%;
    margin: 90px auto;
  }
  .footer {
    text-align: center;
  }
}
</style>
